import evalJS from './transform';

function transformTag(tag) {
  if (tag.indexOf('Switch') > 1) {
    tag = tag.replace(/Switch/g, 'i-switch');
  }
  if (tag.indexOf('Circle') > 1) {
    tag = tag.replace(/Circle/g, 'i-Circle');
  }
  return tag;
}

export default function ({
  template,
  script = 'export default={}',
  styles
}, scope = {}) {
  template = transformTag(template);
  let result={};
  try {
    if (script === 'export default={}' && !template) throw Error('no data')
    result = evalJS(script, scope)

    if (template) {
      result.name = 'renderComponent';
      result.template = `<div id="renderComponent">${template}</div>`; //`<div id="renderComponent"><DatePicker type="date" placeholder="Select date" style="width: 200px"></DatePicker></div>`;
    }
    return {
      result: result,
      styles: styles && styles.join(' ')
    }
  } catch (error) {
    return {
      result: result.template = `<div id="renderComponent">${template}</div>`
    }
  }
}